package com.canaan.business.controller.pc.inOutStock;

import com.canaan.business.domain.inOutStock.WmsInStockDetailOrder;
import com.canaan.business.domain.vo.distributeYardageVo;
import com.canaan.business.service.inOutStock.IWmsInStockDetailOrderService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 入库单明细Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@RequestMapping("/inOutStock/inStockDetailOrder")
public class WmsInStockDetailOrderController extends BaseController
{
    @Resource
    private IWmsInStockDetailOrderService wmsInStockDetailOrderService;


    /**
     * 查询入库单明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WmsInStockDetailOrder wmsInStockDetailOrder)
    {
        startPage();
        List<WmsInStockDetailOrder> list = wmsInStockDetailOrderService.selectWmsInStockDetailOrderList(wmsInStockDetailOrder);
        return getDataTable(list);
    }

    /**
     * 导出入库单明细列表
     */
    @Log(title = "入库单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsInStockDetailOrder wmsInStockDetailOrder)
    {
        List<WmsInStockDetailOrder> list = wmsInStockDetailOrderService.selectWmsInStockDetailOrderList(wmsInStockDetailOrder);
        ExcelUtil<WmsInStockDetailOrder> util = new ExcelUtil<WmsInStockDetailOrder>(WmsInStockDetailOrder.class);
        util.exportExcel(response, list, "入库单明细数据");
    }

    /**
     * 获取入库单明细详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsInStockDetailOrderService.selectWmsInStockDetailOrderById(id));
    }

    /**
     * 新增入库单明细
     */
    @Log(title = "入库单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsInStockDetailOrder wmsInStockDetailOrder)
    {
        if(StringUtils.isEmpty(wmsInStockDetailOrder.getProductNo())){
            throw new ServiceException("物料不能为空");
        }else if(StringUtils.isEmpty(wmsInStockDetailOrder.getOrderNo())){
            throw new ServiceException("单据不能为空");
        }
        WmsInStockDetailOrder select = new WmsInStockDetailOrder();
        select.setOrderNo(wmsInStockDetailOrder.getOrderNo());
        select.setProductNo(wmsInStockDetailOrder.getProductNo());
        List<WmsInStockDetailOrder> list = wmsInStockDetailOrderService.selectWmsInStockDetailOrderList(select);
        if(!list.isEmpty()){
            throw new ServiceException("物料不能重复");
        }
        return toAjax(wmsInStockDetailOrderService.insertWmsInStockDetailOrder(wmsInStockDetailOrder));
    }

    /**
     * 修改入库单明细
     */
    @Log(title = "入库单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsInStockDetailOrder wmsInStockDetailOrder)
    {
        if(StringUtils.isEmpty(wmsInStockDetailOrder.getOrderNo())){
            throw new ServiceException("单据不能为空");
        }else if(StringUtils.isEmpty(wmsInStockDetailOrder.getProductNo())) {
            WmsInStockDetailOrder select = new WmsInStockDetailOrder();
            select.setOrderNo(wmsInStockDetailOrder.getOrderNo());
            select.setProductNo(wmsInStockDetailOrder.getProductNo());
            List<WmsInStockDetailOrder> list = wmsInStockDetailOrderService.selectWmsInStockDetailOrderList(select);
            if (!list.isEmpty()) {
                throw new ServiceException("物料不能重复");
            }
        }
        return toAjax(wmsInStockDetailOrderService.updateWmsInStockDetailOrder(wmsInStockDetailOrder));
    }

    /**
     * 删除入库单明细
     */
    @Log(title = "入库单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsInStockDetailOrderService.deleteWmsInStockDetailOrderByIds(ids));
    }

    /**
     * 分配码货
     */
    @Log(title = "分配码货", businessType = BusinessType.UPDATE)
	@PutMapping("/distributeYardageGoods")
    public AjaxResult distributeYardageGoods(@RequestBody distributeYardageVo entity)
    {
        return toAjax(wmsInStockDetailOrderService.distributeYardageGoods(entity));
    }


    @Log(title = "确认分配", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "确认分配")
    @PutMapping("/confirmDistribute/{id}")
    public AjaxResult confirmDistribute(@PathVariable("id") Long id){
        return toAjax(wmsInStockDetailOrderService.confirmDistribute(id));
    }
}
